$(function(){
	var xsLength="";
		var goodsCookie=$.cookie("goods");
		var goodsKey="";
		var goodsImg="";
		var xmImg='<a href="javascript:;" class=""><img src="img/goodsxq/goods1-r1.jpg" alt="" style="opacity: 1;"></a>'
	console.log(window.location.href.split("?")[1])
	
		console.log(window.location.href.split("?")[1]==undefined)
	if(	window.location.href.split("?")[1]==undefined){
		
		if(goodsCookie.length>1){
			goodsCookie=goodsCookie.split(",");
		}
		console.log(goodsCookie)
		
		
//		console.log(goodsCookie)
		for(var i=0;i<goodsCookie.length;i++){
		
			 goodsKey=goodsCookie[2].split("-")[1];
			$(".goods-fram-title").find("b").text(goodsCookie[0]);
				$(".goods-fram-title").find("a").text(goodsCookie[5].replace(";",""));
			$(".goods-footer-fixd-info").find("h5").text(goodsCookie[0]);
			$("#priceText").text(goodsCookie[1]);
			$(".baseinfo-pice").find("em").text(goodsCookie[4]);
			goodsImg=goodsCookie[2];
			
			$(".goods-fram-left-bot").find("li").each(function(index,ele){
				$(this).find("img").attr("src","img/goodsxq/goods"+goodsKey+"-"+(index+1)+"h.jpg")
			});
			if(goodsCookie[6]!=undefined){$(".goods_breadNavLink").find("a").eq(1).text(goodsCookie[6].replace(";",""))}
			
			 xsLength=goodsCookie[3];
			
		}
		console.log(xsLength)
		for(var k=0;k<xsLength;k++){
				$(".color").append(xmImg);
				$(".color").find("img").eq(k).attr("src","img/goodsxq/goods"+goodsKey+"-r"+(k+1)+".jpg")
			}
			if(goodsKey==3){
				$(".goods-fram-left-bot").find("img").eq(2).css("marginTop","13px")
				$(".goods-fram-left-bot").find("img").eq(3).css("marginTop","13px")
				$(".goods-fram-left-bot").find("img").eq(4).css("marginTop","13px")
			}
			
			if(xsLength=="undefined"||xsLength==""){
//			console.log(111)
			$(".goods-fdj").find("img").attr("src","img/subgoods/"+goodsImg+".jpg");
			$(".goods-lgimg").find("img").attr("src","img/subgoods/"+goodsImg+".jpg");
			$(".goods-footer-nav-img").find("img").attr("src","img/subgoods/"+goodsImg+".jpg");
			$(".goods-fram-left-bot").find("img").attr("src","img/subgoods/"+goodsImg+".jpg");
			
			}else{
			$(".goods-lgimg").find("img").attr("src","img/goodsxq/goods"+goodsKey+"-1l.jpg");
			$(".goods-footer-nav-img").find("img").attr("src","img/goodsxq/goods"+goodsKey+"-"+1+"h.jpg")
			}
	}else{
		var goodshref=window.location.href.split("?")[1].split("&");
		var arrbox=[];
		for(var i=0;i<goodshref.length;i++){
			arrbox.push(goodshref[i].split("=")[1])
		}
		for(var i=0;i<arrbox.length;i++){
			console.log(arrbox[2])
			 goodsKey=arrbox[2].split("-")[1];
			$(".goods-fram-title").find("b").text(decodeURI(arrbox[0]));
				$(".goods-fram-title").find("a").text(decodeURI(arrbox[5]).replace(";",""));
			$(".goods-footer-fixd-info").find("h5").text(decodeURI(arrbox[0]));
			$("#priceText").text(decodeURI(arrbox[1]));
			$(".baseinfo-pice").find("em").text(decodeURI(arrbox[4]));
			goodsImg=decodeURI(arrbox[2]);
			
			$(".goods-fram-left-bot").find("li").each(function(index,ele){
				$(this).find("img").attr("src","img/goodsxq/goods"+goodsKey+"-"+(index+1)+"h.jpg")
			});
			
			
			 xsLength=arrbox[3];
			 
		}
		for(var j=0;j<xsLength;j++){
				$(".color").append(xmImg);
				$(".color").find("img").eq(j).attr("src","img/goodsxq/goods"+goodsKey+"-r"+(j+1)+".jpg")
			}
			if(goodsKey==3){
				$(".goods-fram-left-bot").find("img").eq(2).css("marginTop","13px")
				$(".goods-fram-left-bot").find("img").eq(3).css("marginTop","13px")
				$(".goods-fram-left-bot").find("img").eq(4).css("marginTop","13px")
			}
			console.log(xsLength)
			if(xsLength=="undefined"||xsLength==""){
//			console.log(111)
			$(".goods-fdj").find("img").attr("src","img/subgoods/"+goodsImg+".jpg");
			$(".goods-lgimg").find("img").attr("src","img/subgoods/"+goodsImg+".jpg");
			$(".goods-footer-nav-img").find("img").attr("src","img/subgoods/"+goodsImg+".jpg");
			$(".goods-fram-left-bot").find("img").attr("src","img/subgoods/"+goodsImg+".jpg");
			
			}else{
			$(".goods-lgimg").find("img").attr("src","img/goodsxq/goods"+goodsKey+"-1l.jpg");
			$(".goods-footer-nav-img").find("img").attr("src","img/goodsxq/goods"+goodsKey+"-"+1+"h.jpg")
			}
	}
	
	
	
	
	$(".goods-fram-left-bot").find("li").each(function(index,ele){
		$(this).click(function(){
			$(this).addClass("goodsxsHover").siblings().removeClass("goodsxsHover");
			$(".goods-lgimg").find("img").attr("src",$(this).find("img").attr("src").replace("h","l"));
			$(".goods-fdj img").attr("src",$(this).find("img").attr("src").replace("h","l"))
		})
		
	})
	$(".goods-lgimg").find("img").attr("src",$(".goodsxsHover").find("img").attr("src").replace("h","l"));
			$(".goods-fdj img").attr("src",$(".goodsxsHover").find("img").attr("src").replace("h","l"))
	$(".goods-fram-left-bot").find("li").eq(0).addClass("goodsxsHover").siblings().removeClass("goodsxsHover");
	$(".goods-fram-left-bot").find("li").each(function(index,ele){
		$(this).click(function(){
			$(this).addClass("goodsxsHover").siblings().removeClass("goodsxsHover");
			$(".goods-lgimg").find("img").attr("src",$(this).find("img").attr("src").replace("h","l"));
			$(".goods-fdj img").attr("src",$(this).find("img").attr("src").replace("h","l"))
		})
		
	})
		$(".color").find("a").click(function(){
		$(".goods-lgimg").find("img").attr("src",$(this).find("img").attr("src").replace("r","h"));
		$(this).addClass("selected").siblings().removeClass("selected")
	})
		$(".goods-fram-size").find("a").click(function(){
			$(this).addClass("selected").siblings().removeClass("selected")
		})
	//放大镜
	$(".goods-lgimg").on({
		mouseover:function(){
			$(".goods_bigPicMark").show();
			$(".goods-fdj").show();
		},
		mouseout:function(){
			$(".goods_bigPicMark").hide();
		$(".goods-fdj").hide();
		},
		mousemove:function(evt){
			var e=window.event||evt;
			var left=e.clientX-$(".goods-lgimg").offset().left-$(".goods_bigPicMark").width()/2;
			var top=e.clientY-($(".goods-lgimg").offset().top)+$(".goods_bigPicMark").height()/2-40;
			if(left<0){
				left=0;
			}else if(left>$(".goods-lgimg").width()-$(".goods_bigPicMark").width()){
				left=$(".goods-lgimg").width()-$(".goods_bigPicMark").width();
			}
			if(top<0){
				top=0;
			}else if(top>$(".goods-lgimg").height()-$(".goods_bigPicMark").height()){
				top=$(".goods-lgimg").height()-$(".goods_bigPicMark").height();
			}
			$(".goods_bigPicMark").css("left",left)
			$(".goods_bigPicMark").css("top",top)
//			console.log(top)
			var scaleX=left/($(".goods-lgimg").width()-$(".goods_bigPicMark").width());
			var scaleY=top/($(".goods-lgimg").height()-$(".goods_bigPicMark").height());
			$(".goods-fdj").find("img").css("left",-scaleX*($(".goods-fdj img").width()-$(".goods-fdj").width()))
			$(".goods-fdj").find("img").css("top",-scaleY*($(".goods-fdj img").height()-$(".goods-fdj").height()))
		}
	})
	//滚动固定

	
	$(window).scroll(function(){
		if($(window).scrollTop()>=900){
			$(".goods-footer-nav").css({
				position:"fixed",
				    top: 0,
				    background:"#fff",
				    zIndex:5,
				    paddingLeft:0,
				        left:" 162px",
				            width: "1110px"
			})
			$(".goods-footer-fixd").css("display","block");
			$(".goods-addToCart").show()
		}else if($(window).scrollTop()<900){
			$(".goods-footer-nav").css({
				position:"relative",
				    top: 0,
				    background:"",
				    zIndex:5,
				    paddingLeft:"120px",
				    left:0,
				    width:"1000px"
			})
			$(".goods-footer-fixd").css("display","none");
			$(".goods-addToCart").hide()
		}
	})
	//添加数量
	$(".mbshop_detail_buyNum_less").click(function(){
		
				if(parseInt($(this).next("input").val())>0){
		$(this).next("input").val(parseInt($(this).next("input").val())-1);
//		return false
		}
	})
	$(".mbshop_detail_buyNum_add").click(function(){
		$(this).prev("input").val(parseInt($(this).prev("input").val())+1);

	})
//购物车点击
  var num=parseInt($(".shopping").find("b").text());
	
	$(".goods-fram-btn").find("a").click(function(){
			num+=parseInt($(".mbshop_detail_buyNum").val())
		$.cookie("number",num);
//		console.log($.cookie("number"));
		$(".shopping").find("b").text(num);
		
	});
			
		$(".goods-fram-btn").find("a").click(function(){
		if($(".goods-fram-size").find("dd").find("a").hasClass("selected")==false){
			alert("请选择颜色尺寸")
		}else{
			console.log($(".goods-lgimg").find("img").attr("src"))
			  $.ajax({
                url: "http://localhost/reg/addcart.php",
                data: {
                    "proid": $(".goods-lgimg").find("img").attr("src"),
                    "protitle": encodeURI($(".goods-fram-right").find("b").eq(0).text()),
                    "proprice": encodeURI($(".baseinfo-pice").find("strong").text()),
                    "prooldprice":encodeURI($(".baseinfo-pice").find("em").text()),
                    "prosize": $(".selected").text(),
                    "userId": $(".headerLogin").text().split(",")[1],
                    "carnum":parseInt($(".mbshop_detail_buyNum").val()),
                    "userurl":window.location.href.split("?")[1]
                },
                type:"POST",
                success:function (reg) {
                console.log()
                   $(".goods-opnecat").show();
			$(".goods-mask").show();
                }
            })
		
		}
		
	})
		$(".mbshop_openpop_btnGoPay").attr("href","gwc.html")
			$(".mbshop_openpop_close").click(function(){
				$(".goods-opnecat").hide();
			$(".goods-mask").hide();
			})
			$(".mbshop_openpop_btnGoShop").click(function(){
				$(".goods-opnecat").hide();
			$(".goods-mask").hide();
			})
		var cookiestr=$.cookie("his");
		if(cookiestr!=undefined){
					var listr='<li>'+'<a href="">'+'<img src="" style="opacity: 1;">'+'</a>'+'<a href=""></a>'+'<b></b>'+'</li>';
			cookiestr=cookiestr.replace("undefined","");
		cookiestr=cookiestr.split(";");
		var cookiearr=[];
		for(var j=0;j<cookiestr.length;j++){
//		console.log(cookiestr[j])
			if(cookiearr.indexOf(cookiestr[j])==-1){
				cookiearr.push(cookiestr[j]);
			}
		}
		if(cookiearr.length>=6){
			cookiearr.pop();
		}
		
		for(var i=0;i<cookiearr.length-1;i++){
		
			$(".showHistory").find("ul").append(listr);
			$(".showHistory").find("ul").find("li").eq(i).find("img").attr("src",cookiearr[i].split(",")[0])
			$(".showHistory").find("ul").find("li").eq(i).find("a").eq(1).text(cookiearr[i].split(",")[1])
			$(".showHistory").find("ul").find("li").eq(i).find("b").text(cookiearr[i].split(",")[2])
		}
		}
		console.log(cookiestr)

	
	
		
})
